﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using EShop.BLL;
using EShop.Common;

namespace EShop.Client
{
    public partial class frmMerchandiseBindDetail : frmBase
    {
        public frmMerchandiseBindDetail(string BindID,string MerchandiseName)
        {
            InitializeComponent();
            this.BindID = BindID;
            this.MerchandiseName = MerchandiseName;
        }

        private string BindID;
        private string MerchandiseName;

        private bool IsChanged = false;

        MerchandiseBindBLL oMerchandiseBindBLL = new MerchandiseBindBLL();

        private void frmMerchandiseBindDetail_Load(object sender, EventArgs e)
        {
            this.lblBarcode.Text = BindID;
            this.lblMearchandiseName.Text = MerchandiseName;
            Init();
        }
        private void Init()
        {
            DataTable dt = oMerchandiseBindBLL.MerchandiseBindDetailQuery(BindID, null);
            dgvBindDetail.Rows.Clear();
            int index = -1;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                index = dgvBindDetail.Rows.Add();
                dgvBindDetail.Rows[index].Cells["Barcode"].Value = dt.Rows[i]["Barcode"];
                dgvBindDetail.Rows[index].Cells["MerchandiseNames"].Value = dt.Rows[i]["MerchandiseName"];
                dgvBindDetail.Rows[index].Cells["UnitName"].Value = dt.Rows[i]["UnitName"];
                dgvBindDetail.Rows[index].Cells["Quantity"].Value = dt.Rows[i]["Quantity"];
                dgvBindDetail.Rows[index].Cells["SalePrice"].Value = Common.FormatManager.FormatDecimal(Convert.ToDecimal(dt.Rows[i]["SalePrice"]));
            }
        }

        private void txtBarcode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (string.IsNullOrEmpty(txtBarcode.Text)) return;
                txtBarcode.SelectAll();
                txtBarcode.Focus();
                if (oMerchandiseBindBLL.MerchandiseBindDetailAppend(BindID, txtBarcode.Text.Trim(), (int)txtNum.Value) == false)
                {
                    return;
                }
                DataTable dt = oMerchandiseBindBLL.MerchandiseBindDetailQuery(BindID, txtBarcode.Text);
                int index = -1;
                //dgvBindDetail.Rows.Clear();
                index = dgvBindDetail.Rows.Add();
                dgvBindDetail.Rows[index].Cells["Barcode"].Value = dt.Rows[0]["Barcode"];
                dgvBindDetail.Rows[index].Cells["MerchandiseNames"].Value = dt.Rows[0]["MerchandiseName"];
                dgvBindDetail.Rows[index].Cells["UnitName"].Value = dt.Rows[0]["UnitName"];
                dgvBindDetail.Rows[index].Cells["Quantity"].Value = dt.Rows[0]["Quantity"];
                //string s = Common.FormatManager.FormatDecimal(Convert.ToDecimal(dt.Rows[i]["SalePrice"]));
                dgvBindDetail.Rows[index].Cells["SalePrice"].Value = Common.FormatManager.FormatDecimal(Convert.ToDecimal(dt.Rows[0]["SalePrice"]));

                this.IsChanged = true;
            }
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmMerchandiseBindDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (IsChanged) this.DialogResult = DialogResult.OK;
            else this.DialogResult = DialogResult.Cancel;
        }

        private void 删除指定商品ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string strBarcode = string.Empty;
            try
            {
                strBarcode = dgvBindDetail.SelectedRows[0].Cells["Barcode"].Value.ToString();
            }
            catch
            { }
            if (string.IsNullOrEmpty(strBarcode)) return;
            string strMsg = string.Empty;
            strMsg += "您确定要删除" + strBarcode + "吗?";
            if (MessageBox.Show(strMsg, "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                if (oMerchandiseBindBLL.MerchandiseBindDetailDelete(BindID, strBarcode))
                {
                    MessageBox.Show("操作成功");
                    IsChanged = true;
                    Init();
                }
                else
                {
                    MessageBox.Show("操作失败!");
                }
            }
        }
    }
}
